package com.gitee.qdbp.general.system.web.controlling.handler;

import java.lang.reflect.Field;
import com.gitee.qdbp.able.beans.AcceptAttrs;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.base.enums.OperateType;
import com.gitee.qdbp.base.model.consts.Keys;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.system.service.IDefaultValueConfig;
import com.gitee.qdbp.general.common.api.sequence.model.SimpleSequenceBean;
import com.gitee.qdbp.general.common.api.sequence.model.StrongSequenceBean;

public class TenantCodeAcceptHandler extends AcceptAttrsSimpleHandler<String> implements IDefaultValueConfig.Aware {

    private IDefaultValueConfig defaultValues;

    public TenantCodeAcceptHandler() {
        super();
        this.addWeakAccept(SimpleSequenceBean.TABLE);
        this.addWeakAccept(StrongSequenceBean.TABLE);
    }

    @Override
    public void setDefaultValueConfig(IDefaultValueConfig defaultValues) {
        this.defaultValues = defaultValues;
    }

    @Override
    protected String getUserAttrs(Object o, Field field, String dataType, OperateType operateType, IAccount user) {
        return user.getTenantCode();
    }

    /** 用户已登录的授权方式 **/
    @Override
    protected AcceptAttrs<String> getCurrentAcceptAttrs(Object o, Field field, String dataType, OperateType operateType,
            String tenantCode) throws ServiceException {

        AcceptAttrs<String> attrs = new AcceptAttrs<>();
        attrs.setStarts(false);
        if (isWeakAccept(dataType)) {
            attrs.setAll(true);
            attrs.setRequired(false);
        } else {
            if (Keys.ROOT_CODE.equals(tenantCode)) {
                attrs.setAll(true);
                attrs.setRequired(false);
            } else {
                attrs.setDefaultValue(tenantCode);
                attrs.addAcceptValue(tenantCode);
                attrs.setRequired(true);
            }
        }
        return attrs;
    }

    /** 用户未登录的授权方式 **/
    @Override
    protected AcceptAttrs<String> getDefaultAcceptAttrs(Object o, Field field, String dataType, OperateType operateType)
            throws ServiceException {

        AcceptAttrs<String> attrs = new AcceptAttrs<>();
        attrs.setDefaultValue(defaultValues.getTenantCode());
        attrs.setRequired(true);
        return attrs;
    }

}
